<template>
    <view class="goods-list">
      <view v-for="(goods,i) in goodsList" :key="i" @click="gotoDetail(goods)">
        <my-goods :goods="goods"></my-goods>
      </view>
    </view>
</template>

<script>
  export default {
    data() {
      return {
        // 请求参数对象
        queryObj: {
        // 查询关键词
        query: '',
        // 商品分类Id
        cid: '',
        // 页码值
         pagenum: 1,
         // 每页显示多少条数据
         pagesize: 10
         },
         goodsList:[],
         total:0 ,
         // 是否正在请求数据
         isloading:false
      };
    },
    onLoad(options){
      console.log(options)
      this.queryObj.query = options.query || '',
      this.queryObj.cid = options.cid || '',
      this.getGoodsList()
    },
    methods:{
      async getGoodsList(cb){
        // 打开节流阀
        this.isloading=true
         const {data:res} = await uni.$http.get('/api/public/v1/goods/search',this.queryObj)
         // 关闭节流阀
         this.isloading=false
         cb&&cb()
         console.log(res)
         if(res.meta.status !== 200) return uni.$showMsg()
         this.goodsList = [...this.goodsList,...res.message.goods]
         this.total = res.message.total
      },
      gotoDetail(item){
        console.log(item.goods_id)
        uni.navigateTo({
          url:'/subpkg/good_detail/good_detail?goods_id='+item.goods_id
        })
      }
    },
    // 上拉触底事件
    onReachBottom() {
      // 判断是否还有下一页数据
      if(this.queryObj.pagesize * this.queryObj.pagenum >= this.total) return uni.$showMsg('数据加载完毕')
      // 判断是否正在请求数据,如果是,则不发起其他请求
      if(this.isloading) return
      // 页数+1
      this.queryObj.pagenum++
      this.getGoodsList()
    },
   // 下拉刷新
    onPullDownRefresh() {
      // 1、重置关键数据
      this.queryObj.pagenum=1
      this.total=0
      this.isloading=false
      this.goodsList=[]
      // 2、重新发起请求
      this.getGoodsList(()=>uni.stopPullDownRefresh())
    }
  }
</script>

<style lang="scss">

</style>
